var itemId = "";
var type = "";
var jsonData = {};
var opName = "Phone_GetStore_In_Sub";
var sendName = "Phone_SetStore_In_Finish";
var listData = [];
var checkedRFID = '';
(function(e) {

	mui.plusReady(function() {
		itemId = getQueryString("itemId");

		if(!isEmpty(itemId)) {
			$("#JobCode").html(itemId);
		}

		type = getQueryString("type");
		if(type == "出库操作") {
			opName = "Phone_GetStore_Out_Sub";
			sendName = "Phone_SetStore_Out_Finish";
			$('.mui-title').html('出库操作');
			$('.code-name').text('出库单号：');
			$('#first').html('下架');
			$('#second').html('验证').hide();
			$('#lastBtn').html('出库完成');
			$(".out-sid").show();
			
			// 已下发按钮隐藏
			$('.issued-toggle').hide();
		}
		
		getGetStoreSub(opName);
	});

})(mui);

function getGetStoreSub(opName) {

	var jsonData = {
		op: opName,
		JobCode: itemId
	}

	if(checkParams(jsonData)) {
		mui.toast("请求失败,请稍后再试");
		return;
	}
	var opts = {
		url: "",
		addressUrl: "testline",
		requestType: "POST",
		pastDate: jsonData
	};

	console.log(JSON.stringify(opts));
	
	$('#infoList').html('');

	plus.nativeUI.showWaiting();
	crossDomainAjax(opts, function(data) {
		console.log(JSON.stringify(data));
		plus.nativeUI.closeWaiting();
		$('#infoList').html('');
		if(!isEmpty(data)) {
			for(var i = 0; i < data.length; i++) {
				createItem(i, data[i]);
			}
		} else {
			mui.toast("暂无数据");
		}

	});
}

function createItem(key, item) {
	var status = item.State;

	if(isEmpty(status)) {
		status = "未下发";
	}
	var disabled = 'disabled="true"';
	if(status == "未下发") {
		disabled = "";
		var addClass = '';
	} else if(type != "出库操作") {
		var addClass = 'issued';
	}
	
	var html = '<li id="'+item.ID+'" class="list-item '+addClass+'" data-rfid="'+(item.RFID||'')+'" data-key="'+key+'">'
		+ 
		(
			type != "出库操作" ? '<img class="select-btn" src="../../../ui/index/images/unselect.png" />' : ''
		) +
		
		'<span>货物名称：</span><em>' + item.GoodName + '</em><br />'+
		(
			item.RFID ? '<span>托号：</span><em>' + item.RFID + '</em><br />' : ''
		) + 
		(
			item.SS_SPPlace ? '<span>货位：</span><em>' + item.SS_SPPlace + '</em><br />' : ''
		) + 
		'<span>规格：</span><em>' + item.GoodSpec + '</em><br />' +
		'<span>数量：</span><em><input type="number" ' + disabled + '  value="' + item.Num + '" />'+item.SIS_SecondUnit+'</em><br />' +
		'<span>状态：</span><em>' + status + '</em></li>';
	$("#infoList").append(html);
};

// 选择
$('.list-container').on('tap', '.list-item', function () {
	var rfid = $(this).data('rfid');
	if(rfid) {
		checkedRFID = rfid;
		$('#infoList img').attr('src', '../../../ui/index/images/unselect.png');
		$('.list-item[data-rfid="'+rfid+'"] img').attr('src', '../../../ui/index/images/select.png');
	} else {
		var imgsrc = $(this).find('img').attr('src');
		if(imgsrc.indexOf("unselect") > -1) {
			$(this).find('img').attr('src', '../../../ui/index/images/select.png');
		} else {
			$(this).find('img').attr('src', '../../../ui/index/images/unselect.png');
		}
	}
});

// 显示隐藏已下发数据
$('.issued-toggle').on('tap', function () {
	$('#infoList, .issued-toggle').toggleClass('issued-show');
});

$("#second").on('tap', function() {
	change(this.innerHTML);
});

$("#first").on('tap', function() {
	change(this.innerHTML);
});
// 刷新
$("#refresh").on('tap', function() {
	refresh();
});

$("#lastBtn").on('tap', function() {
	sendSave(sendName);
});

function change(name) {
	var selectStr = getSelectDate();

	if((name == '下发') && isEmpty(selectStr)) {
		mui.toast("请选择货物信息！");
		return;
	}
	
	pageChange({
		pageUrl: "scaninfo.html",
		createNew: true,
		postData: {
			pageflag: "wearhoursInfo.html",
			typeStr: type,
			JobCode: itemId,
			selectStr: JSON.stringify(selectStr),
			rfid: checkedRFID,
			btnName: name
		},
		aniShow: "fade-in"
	});
}

var pageDate = {};

pageDate["selectList"] = [];

function getSelectDate() {
	var images = $("#infoList li");
	pageDate["selectList"] = [];
	for(var i = 0; i < images.length; i++) {
		item = images[i];

		if($(item).find("img").length && $(item).find("img").attr("src").indexOf("unselect") > -1) {
			continue;
		}

		var ID = item.id;
		var num = $(item).find("input").val();
		pageDate["selectList"].push({
			Num: num,
			ID: ID
		});
	}
	console.log(JSON.stringify(pageDate["selectList"]));
	return pageDate["selectList"];

}

function sendSave(sendName) {
	var jsonData = {
		op: sendName,
		JobCode: itemId,
		Pid: userInfo.Pid
	}
	if(checkParams(jsonData)) {
		mui.toast("请求失败,请稍后再试");
		return;
	}
	var opts = {
		url: "",
		addressUrl: "testline",
		requestType: "POST",
		pastDate: jsonData
	};

	console.log(JSON.stringify(opts));

	plus.nativeUI.showWaiting();
	crossDomainAjax(opts, function(data) {
		console.log(JSON.stringify(data));
		plus.nativeUI.closeWaiting();
		
		var pw = plus.webview.currentWebview().opener();
		pw.evalJS("refresh()");
		mui.back();
	});
}


function refresh(){
	removeLi("infoList");
	getGetStoreSub(opName);
}
